package grafica;

import hotel.Autenticazione;
import hotel.Database;

import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.*;
import javax.swing.border.BevelBorder;

import server.AutenticaRegistrazione;

import java.awt.*;
import java.awt.event.*;
import java.awt.Window.Type;
import java.io.IOException;
import java.sql.SQLException;

public class RegistrazioneGrafica {

	private JFrame frmRegistrazione;
	private JTextField usernameT;
	private JPasswordField passwordT;
	private JPasswordField rPasswordT;
	private JTextField emailT;
	private JTextField numTelT;
	private JTextField codiceT;
	private JLabel password, rPassword, username, email, numTel, codice,
			livSicurezza;
	private JButton registrati;
	private JButton invia;
	private Database d;
	private JLabel caricamento;

	public RegistrazioneGrafica(Database d) throws SQLException, IOException {
		this.d = d;
		initialize();
	}

	private void initialize() {
		frmRegistrazione = new JFrame();
		frmRegistrazione.setVisible(true);
		frmRegistrazione.getContentPane().setBackground(Color.LIGHT_GRAY);
		frmRegistrazione.getContentPane().setCursor(
				Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		frmRegistrazione.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		String look = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(look);
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		frmRegistrazione.setTitle("Registrazione");
		frmRegistrazione.setResizable(false);
		frmRegistrazione.setBackground(Color.LIGHT_GRAY);
		frmRegistrazione.setBounds(100, 100, 450, 350);
		frmRegistrazione.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (consensoUscita()) {
					d.chiudiSocket();
					frmRegistrazione.dispose();
				}
			}
		});
		SpringLayout springLayout = new SpringLayout();
		frmRegistrazione.getContentPane().setLayout(springLayout);

		registrati = new JButton("Registrati");
		springLayout.putConstraint(SpringLayout.WEST, registrati, 163,
				SpringLayout.WEST, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, registrati, -29,
				SpringLayout.SOUTH, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, registrati, -166,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		registrati.setBackground(Color.LIGHT_GRAY);
		registrati.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (controlloBase()) {
					String ps = String.valueOf(passwordT.getPassword());
					attivaCaricamentoReg();
					new LoadReg(usernameT.getText(), ps, emailT.getText(),
							numTelT.getText(), d).start();
				}
			}
		});
		registrati.setFont(new Font("Times New Roman", Font.BOLD, 16));
		frmRegistrazione.getContentPane().add(registrati);

		password = new JLabel("Password");
		password.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRegistrazione.getContentPane().add(password);

		rPassword = new JLabel("Ripeti Password");
		springLayout.putConstraint(SpringLayout.EAST, rPassword, 0,
				SpringLayout.EAST, password);
		rPassword.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRegistrazione.getContentPane().add(rPassword);

		username = new JLabel("Username");
		springLayout.putConstraint(SpringLayout.WEST, username, 108,
				SpringLayout.WEST, frmRegistrazione.getContentPane());
		username.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRegistrazione.getContentPane().add(username);

		email = new JLabel("Email");
		springLayout.putConstraint(SpringLayout.EAST, email, 0,
				SpringLayout.EAST, password);
		email.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRegistrazione.getContentPane().add(email);

		numTel = new JLabel("Numero di telefono");
		springLayout.putConstraint(SpringLayout.EAST, numTel, 0,
				SpringLayout.EAST, password);
		numTel.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRegistrazione.getContentPane().add(numTel);

		codice = new JLabel("Codice");
		springLayout.putConstraint(SpringLayout.SOUTH, codice, -6, SpringLayout.NORTH, registrati);
		codice.setVisible(false);
		codice.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRegistrazione.getContentPane().add(codice);

		usernameT = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, username, 0,
				SpringLayout.NORTH, usernameT);
		springLayout.putConstraint(SpringLayout.EAST, username, -21,
				SpringLayout.WEST, usernameT);
		springLayout.putConstraint(SpringLayout.NORTH, usernameT, 25,
				SpringLayout.NORTH, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, usernameT, 190,
				SpringLayout.WEST, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, usernameT, -78,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		usernameT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		frmRegistrazione.getContentPane().add(usernameT);
		usernameT.setColumns(10);
		bordoNormale(usernameT);

		passwordT = new JPasswordField();
		springLayout.putConstraint(SpringLayout.NORTH, password, 1,
				SpringLayout.NORTH, passwordT);
		springLayout.putConstraint(SpringLayout.EAST, password, -22,
				SpringLayout.WEST, passwordT);
		springLayout.putConstraint(SpringLayout.NORTH, passwordT, 6,
				SpringLayout.SOUTH, usernameT);
		springLayout.putConstraint(SpringLayout.WEST, passwordT, 0,
				SpringLayout.WEST, usernameT);
		springLayout.putConstraint(SpringLayout.EAST, passwordT, -78,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		passwordT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		passwordT.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				char[] c = passwordT.getPassword();
				int upper = 0;
				int number = 0;
				if (c.length > 4) {
					livSicurezza.setVisible(true);
					for (int i = 0; i < c.length; i++) {
						if ((Character.isUpperCase(c[i]))) {
							upper++;
						}
						if ((Character.isDigit(c[i]))) {
							number++;
						}
					}
					if (upper > 0 && number > 0) {
						livSicurezza.setText("Efficace");
						livSicurezza.setForeground(new Color(0, 128, 0));
					} else if (upper > 0 || number > 0) {
						livSicurezza.setText("Normale");
						livSicurezza.setForeground(Color.YELLOW);
					} else {
						livSicurezza.setText("Debole");
						livSicurezza.setForeground(Color.RED);
					}
				} else
					livSicurezza.setVisible(false);
			}
		});
		frmRegistrazione.getContentPane().add(passwordT);
		bordoNormale(passwordT);

		rPasswordT = new JPasswordField();
		springLayout.putConstraint(SpringLayout.NORTH, rPassword, 1,
				SpringLayout.NORTH, rPasswordT);
		springLayout.putConstraint(SpringLayout.NORTH, rPasswordT, 6,
				SpringLayout.SOUTH, passwordT);
		springLayout.putConstraint(SpringLayout.WEST, rPasswordT, 0,
				SpringLayout.WEST, usernameT);
		springLayout.putConstraint(SpringLayout.EAST, rPasswordT, -78,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		rPasswordT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		frmRegistrazione.getContentPane().add(rPasswordT);
		bordoNormale(rPasswordT);

		emailT = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, emailT, 22,
				SpringLayout.EAST, email);
		springLayout.putConstraint(SpringLayout.EAST, emailT, -78,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, email, 1,
				SpringLayout.NORTH, emailT);
		springLayout.putConstraint(SpringLayout.NORTH, emailT, 6,
				SpringLayout.SOUTH, rPasswordT);
		emailT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		frmRegistrazione.getContentPane().add(emailT);
		emailT.setColumns(10);
		bordoNormale(emailT);

		numTelT = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, numTel, 1,
				SpringLayout.NORTH, numTelT);
		springLayout.putConstraint(SpringLayout.NORTH, numTelT, 6,
				SpringLayout.SOUTH, emailT);
		springLayout.putConstraint(SpringLayout.WEST, numTelT, 0,
				SpringLayout.WEST, usernameT);
		springLayout.putConstraint(SpringLayout.EAST, numTelT, 0,
				SpringLayout.EAST, usernameT);
		numTelT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		frmRegistrazione.getContentPane().add(numTelT);
		numTelT.setColumns(10);
		bordoNormale(numTelT);

		codiceT = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, codice, -22, SpringLayout.WEST, codiceT);
		springLayout.putConstraint(SpringLayout.WEST, codiceT, 190,
				SpringLayout.WEST, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, codiceT, -68,
				SpringLayout.SOUTH, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, registrati, 6,
				SpringLayout.SOUTH, codiceT);
		springLayout.putConstraint(SpringLayout.EAST, codiceT, -190,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		codiceT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		codiceT.setVisible(false);
		frmRegistrazione.getContentPane().add(codiceT);
		codiceT.setColumns(5);
		bordoNormale(codiceT);

		livSicurezza = new JLabel("Debole");
		springLayout.putConstraint(SpringLayout.NORTH, livSicurezza, 4,
				SpringLayout.NORTH, password);
		springLayout.putConstraint(SpringLayout.WEST, livSicurezza, 370,
				SpringLayout.WEST, frmRegistrazione.getContentPane());
		livSicurezza.setHorizontalAlignment(SwingConstants.CENTER);
		livSicurezza.setForeground(Color.RED);
		livSicurezza.setVisible(false);
		livSicurezza.setFont(new Font("Times New Roman", Font.BOLD, 12));
		frmRegistrazione.getContentPane().add(livSicurezza);

		JLabel minCaratteri = new JLabel("(Min. 5 caratteri)");
		springLayout.putConstraint(SpringLayout.NORTH, minCaratteri, 58,
				SpringLayout.NORTH, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, minCaratteri, 19,
				SpringLayout.WEST, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, minCaratteri, -11,
				SpringLayout.NORTH, rPassword);
		springLayout.putConstraint(SpringLayout.EAST, minCaratteri, -6,
				SpringLayout.WEST, password);
		minCaratteri.setFont(new Font("Times New Roman", Font.PLAIN, 10));
		frmRegistrazione.getContentPane().add(minCaratteri);

		invia = new JButton("Invia");
		springLayout.putConstraint(SpringLayout.WEST, invia, 6,
				SpringLayout.EAST, codiceT);
		springLayout.putConstraint(SpringLayout.SOUTH, invia, -6, SpringLayout.NORTH, registrati);
		springLayout.putConstraint(SpringLayout.EAST, invia, -125,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		invia.setBackground(Color.LIGHT_GRAY);
		invia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (codiceT.getText().equals("")) {
					JOptionPane
							.showMessageDialog(null,
									"Inserire il codice per continuare l'installazione");
				} else {
					String pass = String.valueOf(passwordT.getPassword());
					attivaCaricamentoCod();
					new LoadCod(usernameT.getText(), pass, emailT.getText(),
							numTelT.getText(), codiceT.getText(),
							frmRegistrazione, d).start();
				}
			}			   
		});
		invia.setFont(new Font("Times New Roman", Font.BOLD, 11));
		frmRegistrazione.getContentPane().add(invia);

		JLabel facoltativo = new JLabel("(facoltativo)");
		springLayout.putConstraint(SpringLayout.EAST, livSicurezza, 0,
				SpringLayout.EAST, facoltativo);
		springLayout.putConstraint(SpringLayout.NORTH, facoltativo, 5,
				SpringLayout.NORTH, numTel);
		springLayout.putConstraint(SpringLayout.WEST, facoltativo, 6,
				SpringLayout.EAST, numTelT);
		facoltativo.setFont(new Font("Times New Roman", Font.PLAIN, 10));
		frmRegistrazione.getContentPane().add(facoltativo);

		final JLabel giaRegistrato = new JLabel("Sei gia' registrato?");
		giaRegistrato.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		giaRegistrato.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.NORTH, giaRegistrato, 6,
				SpringLayout.SOUTH, registrati);
		springLayout.putConstraint(SpringLayout.WEST, giaRegistrato, 0,
				SpringLayout.WEST, registrati);
		springLayout.putConstraint(SpringLayout.EAST, giaRegistrato, 0,
				SpringLayout.EAST, registrati);
		giaRegistrato.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new AccessoGrafica(d);
				frmRegistrazione.dispose();
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				giaRegistrato.setForeground(Color.YELLOW);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				giaRegistrato.setForeground(Color.BLUE);
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}
		});
		giaRegistrato.setForeground(Color.BLUE);
		giaRegistrato.setFont(new Font("Times New Roman", Font.PLAIN, 11));
		frmRegistrazione.getContentPane().add(giaRegistrato);

		ImageIcon imageIcon = new ImageIcon("Risorse/indicator.gif");
		caricamento = new JLabel("Connessione al server...", imageIcon,
				JLabel.CENTER);
		springLayout.putConstraint(SpringLayout.SOUTH, caricamento, -118,
				SpringLayout.SOUTH, frmRegistrazione.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, invia, 26, SpringLayout.SOUTH, caricamento);
		springLayout.putConstraint(SpringLayout.EAST, caricamento, -144,
				SpringLayout.EAST, frmRegistrazione.getContentPane());
		frmRegistrazione.getContentPane().add(caricamento);
		caricamento.setVisible(false);
		invia.setVisible(false);
	}

	private void bordoRosso(JTextField t) {
		t.setBorder(BorderFactory.createLineBorder(Color.RED));
	}

	private void bordoNormale(JTextField t) {
		t.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
	}

	private boolean controlloBase() {
		tuttiBordiNormali();
		String pass = String.valueOf(passwordT.getPassword());
		String rPass = String.valueOf(rPasswordT.getPassword());
		if (usernameT.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "Devi inserire l'username!");
			usernameT.setBorder(BorderFactory.createLineBorder(Color.RED));
			return false;
		} else if (pass.equals("") || pass.length() < 5) {
			JOptionPane.showMessageDialog(null,
					"Devi inserire la password! (Minimo 5 caratteri)");
			passwordT.setBorder(BorderFactory.createLineBorder(Color.RED));
			return false;
		} else if (rPass.equals("")) {
			JOptionPane.showMessageDialog(null,
					"Devi inserire la password correttamente!");
			rPasswordT.setBorder(BorderFactory.createLineBorder(Color.RED));
			return false;
		} else if (!pass.equals(rPass)) {
			JOptionPane.showMessageDialog(null, "La password e' errata");
			rPasswordT.setBorder(BorderFactory.createLineBorder(Color.RED));
			return false;
		} else if (emailT.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "Devi inserire la tua email!");
			emailT.setBorder(BorderFactory.createLineBorder(Color.RED));
			return false;
		}
		return true;
	}

	private void tuttiBordiNormali() {
		usernameT.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED));
		passwordT.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED));
		rPasswordT.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED));
		emailT.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		numTelT.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
	}

	public void attivaCaricamentoReg() {
		caricamento.setVisible(true);
		registrati.setVisible(false);
	}

	public void disattivaCaricamentoReg() {
		caricamento.setVisible(false);
		registrati.setVisible(true);
	}

	public void attivaCaricamentoCod() {
		caricamento.setVisible(true);
		invia.setVisible(false);
	}

	public void disattivaCaricamentoCod() {
		caricamento.setVisible(false);
		invia.setVisible(true);
	}

	private boolean consensoUscita() {
		int option = JOptionPane
				.showConfirmDialog(
						null,
						"Continuare ? Uscendo non si potra' continuare con la registrazione!",
						"Attenzione", JOptionPane.YES_NO_OPTION);
		return option == JOptionPane.YES_OPTION;
	}

	class LoadReg extends Thread {
		private String username, password, email, numTel;
		private Database d;

		public LoadReg(String username, String password, String email,
				String numTel, Database d) {
			this.username = username;
			this.password = password;
			this.email = email;
			this.numTel = numTel;
			this.d = d;
		}

		public void run() {
			if (d.registra(username, password, email, numTel)) {
				codice.setVisible(true);
				codiceT.setVisible(true);
				invia.setVisible(true);
				registrati.setVisible(false);
				usernameT.setEnabled(false);
				passwordT.setEnabled(false);
				rPasswordT.setEnabled(false);
				emailT.setEnabled(false);
				numTelT.setEnabled(false);
			}
			disattivaCaricamentoReg();
		}
	}

	class LoadCod extends Thread {
		private String username, pass, email, numTel, codice;
		private Database d;
		private JFrame r;

		public LoadCod(String username, String pass, String email,
				String numTel, String codice, JFrame r, Database d) {
			this.username = username;
			this.pass = pass;
			this.email = email;
			this.numTel = numTel;
			this.codice = codice;
			this.d = d;
			this.r = r;
		}

		public void run() {
			if (d.inviaCodice(username, pass, email, numTel, codice)) {
				new AccessoGrafica(d);
				r.dispose();
			}
			disattivaCaricamentoCod();
		}
	}
}
